package br.usp.pcs.acs.negocios.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.usp.pcs.acs.negocios.dao.interfaces.CADaoInterface;
import br.usp.pcs.acs.negocios.dto.CADto;


public class CADao implements CADaoInterface
{
	@Override
	public void edit(CADto inDto) {		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;		
		
		try{
			conn = DatabaseConnect.getConnection();
			
			pstmt = conn.prepareStatement(CRUD_U);
			
			pstmt.setInt(5, inDto.getId());//id
			pstmt.setString(1,inDto.getNome());
			pstmt.setString(2,inDto.getCNPJ());
			pstmt.setString(3,inDto.getLogin());
			pstmt.setString(4,inDto.getSenha());
			
			pstmt.executeUpdate();
		}
		
		
		
		catch (SQLException ex){
		    // handle any errors
		    System.out.println("SQLException: " + ex.getMessage());
		    System.out.println("SQLState: " + ex.getSQLState());
		    System.out.println("VendorError: " + ex.getErrorCode());
		}
		finally {
		    if (rs != null){
		    	try {rs.close();}catch (SQLException sqlEx) { } // ignore 
		    	rs = null;
		    }
		    if (pstmt != null){
		        try {pstmt.close();}catch (SQLException sqlEx) { } // ignore
		        pstmt = null;
		    }
		    if (conn != null){
		    	try{conn.close();}catch (SQLException sqlEx) { } // ignore
		    }
		}
	}
	@Override
	public ArrayList<CADto> findAll() {		
		return finding(CRUD_R);
	}

	@Override
	public CADto findById(int id) {
		String busca =  "SELECT * FROM ca WHERE id = " + id;
		ArrayList<CADto> ret= finding(busca);
		return ret.isEmpty()?null:ret.get(0);
	}

	@Override
	public void insert(CADto inDto)
	{		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;		
		
		try{
			conn = DatabaseConnect.getConnection();
			
			pstmt = conn.prepareStatement(CRUD_C);
			
			pstmt.setNull(1, java.sql.Types.INTEGER);//id
			pstmt.setString(2,inDto.getNome());
			pstmt.setString(3,inDto.getCNPJ());
			pstmt.setString(4,inDto.getLogin());
			pstmt.setString(5,inDto.getSenha());
			
			pstmt.execute();
		}
		
		
		
		catch (SQLException ex){
		    // handle any errors
		    System.out.println("SQLException: " + ex.getMessage());
		    System.out.println("SQLState: " + ex.getSQLState());
		    System.out.println("VendorError: " + ex.getErrorCode());
		}
		finally {
		    if (rs != null){
		    	try {rs.close();}catch (SQLException sqlEx) { } // ignore 
		    	rs = null;
		    }
		    if (pstmt != null){
		        try {pstmt.close();}catch (SQLException sqlEx) { } // ignore
		        pstmt = null;
		    }
		    if (conn != null){
		    	try{conn.close();}catch (SQLException sqlEx) { } // ignore
		    }
		}
	}
	private ArrayList<CADto> finding(String busca) {
		ArrayList<CADto> LoutDto = new ArrayList<CADto>();
		CADto tmpDto = null;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		
		try{
			conn = DatabaseConnect.getConnection();			
			pstmt = conn.prepareStatement(busca);			
			rs = pstmt.executeQuery();
			
			while(rs.next())
			{
				tmpDto = new CADto();
				tmpDto.setId(rs.getInt(1));
				tmpDto.setNome(rs.getString(2));
				tmpDto.setCNPJ(rs.getString(3));
				tmpDto.setLogin(rs.getString(4));
				tmpDto.setSenha(rs.getString(5));
				
				LoutDto.add(tmpDto);
			}
		}		
		catch (SQLException ex){
		    // handle any errors
		    System.out.println("SQLException: " + ex.getMessage());
		    System.out.println("SQLState: " + ex.getSQLState());
		    System.out.println("VendorError: " + ex.getErrorCode());
		}
		finally {
		    if (rs != null){
		    	try {rs.close();}catch (SQLException sqlEx) { } // ignore 
		    	rs = null;
		    }
		    if (pstmt != null){
		        try {pstmt.close();}catch (SQLException sqlEx) { } // ignore
		        pstmt = null;
		    }
		    if (conn != null){
		    	try{conn.close();}catch (SQLException sqlEx) { } // ignore
		    }
		}
		
		return LoutDto;
	}
	@Override
	public void remove(int id) 
	{
		Connection conn = null;
		PreparedStatement pstmt = null;		
		try{
			conn = DatabaseConnect.getConnection();			
			pstmt = conn.prepareStatement(CRUD_D);			
			pstmt.setInt(1, id);			
			pstmt.execute();
		}		
		catch (SQLException ex){
		    // handle any errors
		    System.out.println("SQLException: " + ex.getMessage());
		    System.out.println("SQLState: " + ex.getSQLState());
		    System.out.println("VendorError: " + ex.getErrorCode());
		}
		finally {
		    if (pstmt != null){
		        try {pstmt.close();}catch (SQLException sqlEx) { } // ignore
		        pstmt = null;
		    }
		    if (conn != null){
		    	try{conn.close();}catch (SQLException sqlEx) { } // ignore
		    }
		}
	}

}
